package com.project.zsxd.server.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.zsxd.server.common.enums.ResultEnum;
import com.project.zsxd.server.common.exception.SystemException;
import com.project.zsxd.server.common.util.CommonUtil;
import com.project.zsxd.server.user.entity.User;
import com.project.zsxd.server.user.mapper.UserMapper;
import com.project.zsxd.server.user.service.IUserService;
import com.project.zsxd.server.user.vo.UserVO;
import com.project.zsxd.server.userRole.entity.UserRole;
import com.project.zsxd.server.userRole.mapper.UserRoleMapper;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author kaler
 * @since 2020-05-12
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public List<UserVO> castToVOList(List<User> userList) throws SystemException{
        List<UserVO> userVOList = new ArrayList<>();
        UserVO userVO;
        for (User user: userList) {
            userVO = new UserVO();
            BeanUtils.copyProperties(user, userVO);
            userVOList.add(userVO);
        }
        return userVOList;
    }

    @Override
    public List<String> queryRolesByUserName(String userName) {
        return userMapper.selectRolesByUserName(userName);
    }

    @Override
    public List<String> queryPermissionsByUserName(String userName) {
        return userMapper.selectPermissionsByUserName(userName);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public User registerOpenUser(User newUser) throws SystemException {
        //校验非空属性
        if (CommonUtil.isEmpty(newUser.getUserName())
                || CommonUtil.isEmpty(newUser.getNickName())
                || CommonUtil.isEmpty(newUser.getPasswd())) {
            throw new SystemException(ResultEnum.ARGUEMENTS_ERROR);
        }
        //密码加密
        newUser.setPasswd(BCrypt.hashpw(newUser.getPasswd(), BCrypt.gensalt()));
        //保存用户
        userMapper.insert(newUser);
        //添加默认角色
        UserRole defaultUserRole = new UserRole();
        defaultUserRole.setRoleCode("OPEN_USER");
        defaultUserRole.setUserName(newUser.getUserName());
        userRoleMapper.insert(defaultUserRole);
        User savedUser = userMapper.selectById(newUser.getUserName());
        return savedUser;
    }

    @Override
    public User queryByOpenId(String openId)  {
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("open_id", openId));
        return user;
    }
}
